package com.scgk.sync.controller;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpATTRS;
import com.scgk.sync.common.BaseResponse;
import com.scgk.sync.common.bak.SFTPChannel;
import com.scgk.sync.common.bak.SFTPGetTest;
import com.scgk.sync.common.constant.SFTPConstants;
import com.scgk.sync.common.utils.SftpClientUtil;
import com.scgk.sync.common.utils.SftpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "测试SFTP", tags = "测试SFTP")
@Slf4j
@RestController
@RequestMapping("/test")
public class TestSFTPController {



    @ApiOperation("从SFTP服务器下载文件")
    @GetMapping("/filesDownload")
    public BaseResponse filesDownload() throws Exception {
        //开启连接
        SftpUtil sftpUtil = new SftpUtil("test", "test", "127.0.0.1", "22");
        sftpUtil.connect();
        try {
            //从服务器下载文件
            InputStream download = sftpUtil.downloadFile("/", "1.png");
            //把文件保存到本地
            File file = new File("F:\\demo\\1.png");

            FileUtils.copyInputStreamToFile(download, file);
            download.close();
            //关闭连接
            sftpUtil.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return BaseResponse.ok();
    }

}
